package com.origin.onlinesys.
        web.controllers.modules.home;

import com.origin.onlinesys.common.controllers.BaseController;
import com.origin.onlinesys.service.modules.user.impl.RolesServiceImpl;
import com.origin.onlinesys.web.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/1/30.
 */
@Controller
public class HomeController extends BaseController {
    @Autowired
    private RolesServiceImpl service;

    @RequestMapping(value = "/home")
    public String home(){
        if(WebUtils.getAccount().getId()==null) return "crosslogin";
        return "index";
    }

    @RequestMapping(value = "/home/dashboard")
    public String dashboard(){
        return "views/dashboard";
    }

    @RequestMapping(value = "/login")
    public String uiHome(){
        if(WebUtils.getAccount().getId()!=null)return "index";
        return "login";
    }
    @RequestMapping(value = "/header")
    public ModelAndView header(){
        String roleName = WebUtils.getAccount().getRoleName();
        Map map = new HashMap();
        map.put("test","admin");
        map.put("zone",roleName);
        if("admin".equals(roleName)){
            map.put("zoneName", WebUtils.getZoneName());
            map.put("zoneData",WebUtils.getAccount().getZoneList());
        }
        map.put("userName", WebUtils.getAccount().getRealname());
        return new ModelAndView("/pages/header","data",map);
    }
    @RequestMapping(value = "/div")
    public String div(){
        return "pages/home";
    }

    @RequestMapping(value = "/nav")
    public String nav(){
        String roleId = WebUtils.getAccount().getRoleId();
        StringBuilder sb = new StringBuilder("navRole");
        sb.append(roleId);sb.append(".html");

        StringBuilder  path = new StringBuilder(this.templetNavPath());
        File file = new File(path.append(sb.toString()).toString());
        if(!file.exists()){
            this.writerHtml("templetNav.html","menu",
                    service.selectRoleMenu(roleId),"navRole"+roleId+".html");
        }
        StringBuilder page = new StringBuilder("pages/");
        page.append("navRole");page.append(roleId);
        return page.toString();
    }
    @RequestMapping(value = "/test")
    public String test(){
        return "test";
    }

    @RequestMapping(value = "/endSession")
    public String endSession(){
        return "endSession";
    }
}
